package com.zttek.photosShareProject.modules.web.share;

import com.baomidou.mybatisplus.plugins.Page;
import com.zttek.photosShareProject.common.base.MyBaseController;
import com.zttek.photosShareProject.common.json.ShareVO;
import com.zttek.photosShareProject.common.json.UserVO;
import com.zttek.photosShareProject.modules.model.Share;
import com.zttek.photosShareProject.modules.service.login.IUserService;
import com.zttek.photosShareProject.modules.service.manager.IKeywordService;
import com.zttek.photosShareProject.modules.service.share.IAbuseService;
import com.zttek.photosShareProject.modules.service.share.IImageService;
import com.zttek.photosShareProject.modules.service.share.IInfoService;
import com.zttek.photosShareProject.modules.service.share.IShareService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@RequestMapping("/share")
@Controller
public class ShareController extends MyBaseController{

    @Autowired
    private IShareService shareService;

    @Autowired
    private IAbuseService abuseService;

    @Autowired
    private IKeywordService keywordService;

    @RequestMapping("/test")
    public String test() throws Exception{
//        shareService.newShare(new long[2],"???", 123);
        return "test/sharetest";
    }

    @RequestMapping(value = "/getDetail", method = RequestMethod.POST)
    @ResponseBody
    public Object getDetail(@RequestParam("shareId") Long shareId, @RequestParam("userId") Long userId) throws Exception {
        Share share = shareService.selectShare(shareId);
        ShareVO vo = shareService.share2VO(share, userId, false);
        return renderSuccess(vo);
    }

    @RequestMapping(value = "/getPublicList", method = RequestMethod.POST)
    @ResponseBody
    public Object getPublicList(@RequestBody ShareVO.GetListForm form) throws Exception {
        Page<Share> page = new Page<>(form.getPageNum(), form.getPageSize(), form.getOrderByField());
        page.setAsc(form.isAscend());
        Long userId = form.getUserId();
        shareService.selectPublicShare(page, userId);
        List<Share> shares = page.getRecords();
        List<ShareVO> list = shareService.share2VO(shares, userId, true);
        return renderSuccess(list);
    }

    @RequestMapping(value = "/getSelfList", method = RequestMethod.POST)
    @ResponseBody
    public Object getSelfList(@RequestBody ShareVO.GetListForm form) throws Exception {
        Page<Share> page = new Page<>(form.getPageNum(), form.getPageSize(), form.getOrderByField());
        page.setAsc(form.isAscend());
        Long userId = form.getUserId();
        shareService.selectSelfShare(page, userId);
        List<Share> shares = page.getRecords();
        List<ShareVO> list = shareService.share2VO(shares, userId, true);
        return renderSuccess(list);
    }

    @RequestMapping(value = "/getHideList", method = RequestMethod.POST)
    @ResponseBody
    public Object getHide(@RequestBody ShareVO.GetListForm form) throws Exception {
        Page<Share> page = new Page<>(form.getPageNum(), form.getPageSize(), form.getOrderByField());
        page.setAsc(form.isAscend());
        Long userId = form.getUserId();
        shareService.selectHideShare(page, userId);
        List<Share> shares = page.getRecords();
        List<ShareVO> list = shareService.share2VO(shares, userId, false);
        return renderSuccess(list);
    }

    @RequestMapping(value = "/getCollectList", method = RequestMethod.POST)
    @ResponseBody
    public Object getCollect(@RequestBody ShareVO.GetListForm form) throws Exception {
        Page<Share> page = new Page<>(form.getPageNum(), form.getPageSize(), form.getOrderByField());
        page.setAsc(form.isAscend());
        Long userId = form.getUserId();
        shareService.selectCollectShare(page, userId);
        List<Share> shares = page.getRecords();
        List<ShareVO> list = shareService.share2VO(shares, userId, true);
        return renderSuccess(list);
    }

    @RequestMapping(value = "/new", method = RequestMethod.POST)
    @ResponseBody
    public Object newShare(@RequestBody ShareVO.createForm form) throws Exception{
        Share share = shareService.newShare(form.getImageIds(), form.getContent(), form.getAtIds(),
                form.getUserId(), form.getSourceId(), form.isHide());
        return renderSuccess(shareService.share2VO(share, form.getUserId(), false));
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public Object delete(@RequestParam("shareId") Long shareId) throws Exception {
        shareService.deleteShare(shareId);
        return renderSuccess();
    }

    @RequestMapping(value = "/praise", method = RequestMethod.POST)
    @ResponseBody
    public Object praise(@RequestBody ShareVO.RelevanceForm form) throws Exception {
        shareService.praise(form.getShareId(), form.getUserId(), form.isDirection());
        return renderSuccess();
    }

    @RequestMapping(value = "/hide", method = RequestMethod.POST)
    @ResponseBody
    public Object hide(@RequestBody ShareVO.RelevanceForm form) throws Exception {
        shareService.hide(form.getShareId(), form.getUserId(), form.isDirection());
        return renderSuccess();
    }

    @RequestMapping(value = "/collect", method = RequestMethod.POST)
    @ResponseBody
    public Object collect(@RequestBody ShareVO.RelevanceForm form) throws Exception {
        shareService.collect(form.getShareId(), form.getUserId(), form.isDirection());
        return renderSuccess();
    }

    @RequestMapping(value = "/abuse", method = RequestMethod.POST)
    @ResponseBody
    public Object abuse(@RequestParam("shareId") Long shareId,
                        @RequestParam("userId") Long userId,
                        @RequestParam("reason") String reason) throws Exception {
        abuseService.abuseShare(shareId, userId, reason);
        return renderSuccess();
    }

    @RequestMapping("/check")
    @ResponseBody
    public Object check(@RequestParam("content") String content) throws Exception {
        Set<String> keyword = keywordService.getContainKeywordSet(content);
        return renderSuccess(keyword);
    }

    @RequestMapping("/getAtList")
    @ResponseBody
    public Object getAtList(@RequestParam("userId") Long userId) throws Exception {
        UserVO[] atList = shareService.atIds2VOs(shareService.getUserStarList(userId));
        return renderSuccess(atList);
    }
}
